import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/exam.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/examination/do_exam_view.dart';

class OfficialExamWidget extends StatelessWidget {
  final List<Exam> examList;
  final Function refresh;

  const OfficialExamWidget({
    Key key,
    @required this.examList,
    @required this.refresh,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: ListTile.divideTiles(
        context: context,
        color: Color.fromRGBO(240, 240, 240, 1),
        tiles: examList.map((exam) {
          return getExamItem(exam, context);
        }),
      ).toList(),
    );
  }

  Widget getExamItem(Exam exam, BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10),
      padding: EdgeInsets.fromLTRB(20, 20, 20, 10),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Color.fromRGBO(240, 240, 240, 1),
            offset: Offset(0, -1),
          ),
          BoxShadow(
            color: Color.fromRGBO(240, 240, 240, 1),
            offset: Offset(0, 1),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          title(exam),
          timeRange(exam),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              status(exam),
              btnDoTest(exam, context),
            ],
          ),
        ],
      ),
    );
  }

  Widget title(Exam exam) {
    return Text(
      exam.name,
      style: TextStyle(
        fontSize: 18,
        color: Colors.black87,
      ),
    );
  }

  Widget timeRange(Exam exam) {
    var format = new DateFormat('yyyy-MM-dd HH:mm');
    var start = Column(
      children: [
        Text(
          '开始',
          style: TextStyle(
            fontSize: 13,
            color: Colors.black87,
          ),
        ),
        Padding(
          padding: EdgeInsets.only(top: 5),
          child: Text(
            format.format(exam.startTime),
            style: TextStyle(
              fontSize: 13,
              color: Colors.black54,
            ),
          ),
        ),
      ],
    );
    var end = Column(
      children: [
        Text(
          '结束',
          style: TextStyle(
            fontSize: 13,
            color: Colors.black87,
          ),
        ),
        Padding(
          padding: EdgeInsets.only(top: 5),
          child: Text(
            format.format(exam.endTime),
            style: TextStyle(
              fontSize: 13,
              color: Colors.black54,
            ),
          ),
        ),
      ],
    );

    return Container(
      margin: EdgeInsets.only(bottom: 10),
      padding: EdgeInsets.fromLTRB(0, 20, 0, 20),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1,
            color: Color.fromRGBO(240, 240, 240, 1),
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          start,
          Image.asset(R.assetsExamRight, height: 10),
          end,
        ],
      ),
    );
  }

  Widget status(Exam exam) {
    Color color;
    String text;
    if (exam.status == ExamStatus.NotStarted) {
      color = Color.fromRGBO(255, 159, 41, 1);
      text = '考试未开始';
    } else if (exam.status == ExamStatus.Started) {
      color = Color.fromRGBO(75, 191, 137, 1);
      text = '考试已开始';
    } else {
      color = Colors.black54;
      text = '考试已结束';
    }

    return Text(
      text,
      style: TextStyle(
        fontSize: 14,
        color: color,
      ),
    );
  }

  Widget btnDoTest(Exam exam, BuildContext context) {
    return GestureDetector(
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(50),
          color: exam.status == ExamStatus.Started ? Theme.of(context).primaryColor : Colors.black12,
        ),
        child: Text(
          '开始考试',
          style: TextStyle(
            fontSize: 12,
            color: Colors.white,
          ),
        ),
      ),
      onTap: () async {
        if (exam.status != ExamStatus.Started) return;
        var confirm = await confirmExam(context);
        if (confirm == null || !confirm) return;

        await Navigator.of(context).push(
          MaterialPageRoute(
            builder: (cxt) => DoExamView(exam: exam),
          ),
        );
        refresh();
      },
    );
  }

  Future<bool> confirmExam(BuildContext context) async {
    return showDialog(
      context: context,
      builder: (cxt) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('您确认进入本场考试吗？'),
          actions: [
            FlatButton(
              child: Text('取消'),
              onPressed: () {
                Navigator.pop(context, false);
              },
            ),
            FlatButton(
              child: Text('确认'),
              onPressed: () {
                Navigator.pop(context, true);
              },
            ),
          ],
        );
      },
    );
  }
}
